/**
 * 
 */
package com.bot.webSite.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Label;

/**
 * @author jmcodol
 *
 */
public class newsletter extends Composite implements HasText {

	private static newsletterUiBinder uiBinder = GWT
			.create(newsletterUiBinder.class);

	interface newsletterUiBinder extends UiBinder<Widget, newsletter> {
	}
	
	@UiField TextBox adresse_email;
	@UiField Button bouton_enregistrer;
	@UiField TextArea message;
	@UiField Label label_adresse;
	@UiField Label label_message;
	private WebSite website;
	private int LANG;

	/**
	 * Because this class has a default constructor, it can
	 * be used as a binder template. In other words, it can be used in other
	 * *.ui.xml files as follows:
	 * <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 *   xmlns:g="urn:import:**user's package**">
	 *  <g:**UserClassName**>Hello!</g:**UserClassName>
	 * </ui:UiBinder>
	 * Note that depending on the widget that is used, it may be necessary to
	 * implement HasHTML instead of HasText.
	 * @param webSite 
	 * @param lANG 
	 */
	public newsletter(WebSite webSite, int LANG) {
//		Timer t = new Timer() {
//			
//			@Override
//			public void run() {
//			}
//		};

		this.LANG = LANG ;
		
		this.website = webSite ;
		initWidget(uiBinder.createAndBindUi(this));

		if (LANG==WebSite.LANG_FR) {
		bouton_enregistrer.setText("s'enregistrer") ;
		label_adresse.setText("adresse email") ;
		label_message.setText("message")       ;
	}
	if (LANG==WebSite.LANG_EN) {
		bouton_enregistrer.setText("send");
		label_adresse.setText("email address") ;
		label_message.setText("message")       ;
	}
		
		bouton_enregistrer.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clickEnregistrer() ;
			}
		}) ; 
		
		bouton_enregistrer.setEnabled(false) ;
		bouton_enregistrer.setTitle("email adress invalid");
		
		
		
		adresse_email.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (adresseEmailValide(adresse_email.getText())) {
					bouton_enregistrer.setEnabled(true);
				} else {
					bouton_enregistrer.setEnabled(false);
				}
			}}) ;
		
	}

	protected void clickEnregistrer() {
		
		if (!adresseEmailValide(adresse_email.getText())) {
			String msgAlert = "" ;
			if (LANG==WebSite.LANG_FR) {
				msgAlert = "adresse email invalide" ;
			}
			if (LANG==WebSite.LANG_EN) {
				msgAlert = "invalid email address" ;
			}
			Window.alert(msgAlert);
			return ;
		}
		String email = adresse_email.getText() ;
		String msg = message.getText() ;
		website.sendNewsLetter(email, msg, this) ;

	}

	private boolean adresseEmailValide(String addresse) {
		if (addresse.contains("@")) {
			return true ;
		}
		return false;
	}

//	public newsletter(String firstName) {
//		initWidget(uiBinder.createAndBindUi(this));
//
//		// Can access @UiField after calling createAndBindUi
//		adresse_email.setText(firstName);
//	}

	public void setText(String text) {
		adresse_email.setText(text);
	}

	/**
	 * Gets invoked when the default constructor is called
	 * and a string is provided in the ui.xml file.
	 */
	public String getText() {
		return adresse_email.getText();
	}

	public void envoiStatus(boolean status) {
		if (status==true) {
			adresse_email.setText("") ;
			message      .setText("") ;
		}
	}

}
